package com.vancouver.knows.ui.fragment.homePage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.ui.activity.MainActivity;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscoutResultActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Chris Li on 2016-11-30.
 */
public class HomepageBusinessView {

    @Bind(R.id.first_image)
    ImageView mFirstImage;
    @Bind(R.id.first_text)
    TextView mFirstText;
    @Bind(R.id.first_item)
    LinearLayout mFirstItem;
    @Bind(R.id.second_image)
    ImageView mSecondImage;
    @Bind(R.id.second_text)
    TextView mSecondText;
    @Bind(R.id.second_item)
    LinearLayout mSecondItem;
    @Bind(R.id.third_image)
    ImageView mThirdImage;
    @Bind(R.id.third_text)
    TextView mThirdText;
    @Bind(R.id.third_item)
    LinearLayout mThirdItem;
    @Bind(R.id.fourth_image)
    ImageView mFourthImage;
    @Bind(R.id.fourth_text)
    TextView mFourthText;
    @Bind(R.id.fourth_item)
    LinearLayout mFourthItem;

    private Context mContext;
    private LayoutInflater mInflater;
    private View mView;

    public HomepageBusinessView(Context context, LayoutInflater inflater) {
        mContext = context;
        mInflater = inflater;
        mView = mInflater.inflate(R.layout.home_businesses_view, null);
        ButterKnife.bind(this, mView);
    }

    public View getBusinessView() {
        return mView;
    }

    @OnClick(R.id.more_text)
    public void onMoreTextClicked() {
        ((MainActivity) mContext).showFragment(R.id.cityDiscountTab, 0);
    }

    /**
     *  Displays the first 2 categories that have the "display_on_home" flag set to "Y". Then randomly picks
     *  two other categories to display.
     *
     * @param discountCategoryInfoCache
     */
    public void init(List<DiscountCategoryInfo> discountCategoryInfoCache) {
        if (discountCategoryInfoCache != null && !discountCategoryInfoCache.isEmpty() && discountCategoryInfoCache.size() > 1) {
            List<DiscountCategoryInfo> otherCategoriesToDisplay = new ArrayList<>();
            int displayCount = 0;
            for (final DiscountCategoryInfo discountCategoryInfo : discountCategoryInfoCache) {
                if (discountCategoryInfo.getDisplayOnHome().equals("Y")) {
                    displayCount++;
                    if (displayCount == 1) {
                        mFirstText.setText(discountCategoryInfo.getName());
                        Picasso.with(mContext).load(discountCategoryInfo.getImageUrl()).fit().into(mFirstImage);
                        mFirstItem.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(mContext, CityDiscoutResultActivity.class);
                                intent.putExtra("index", discountCategoryInfo.getPosition());
                                mContext.startActivity(intent);
                            }
                        });
                    } else if (displayCount == 2) {
                        mSecondText.setText(discountCategoryInfo.getName());
                        Picasso.with(mContext).load(discountCategoryInfo.getImageUrl()).fit().into(mSecondImage);
                        mSecondItem.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(mContext, CityDiscoutResultActivity.class);
                                intent.putExtra("index", discountCategoryInfo.getPosition());
                                mContext.startActivity(intent);
                            }
                        });
                    }
                } else { // equals "N"
                    if (discountCategoryInfo.getPosition() != 0) {
                        otherCategoriesToDisplay.add(discountCategoryInfo);
                    }
                }
            }

            Random random = new Random();
            int firstRandomIndex = random.nextInt(otherCategoriesToDisplay.size());
            final DiscountCategoryInfo firstRandomItem = otherCategoriesToDisplay.remove(firstRandomIndex);
            mThirdText.setText(firstRandomItem.getName());
            Picasso.with(mContext).load(firstRandomItem.getImageUrl()).fit().into(mThirdImage);
            mThirdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, CityDiscoutResultActivity.class);
                    intent.putExtra("index", firstRandomItem.getPosition());
                    mContext.startActivity(intent);
                }
            });

            int secondRandomIndex = random.nextInt(otherCategoriesToDisplay.size());
            final DiscountCategoryInfo secondRandomItem = otherCategoriesToDisplay.remove(secondRandomIndex);
            mFourthText.setText(secondRandomItem.getName());
            Picasso.with(mContext).load(secondRandomItem.getImageUrl()).fit().into(mFourthImage);
            mFourthItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, CityDiscoutResultActivity.class);
                    intent.putExtra("index", secondRandomItem.getPosition());
                    mContext.startActivity(intent);
                }
            });
        }
    }
}
